package com.risetechnology.krystalkitchen1;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.risetechnology.adapter.RecipeListAdapter;
import com.risetechnology.common.Shared;
import com.risetechnology.customviews.CustomFontTextView;
import com.risetechnology.data.Recipe;
import com.risetechnology.master.NavigationActivity;

public class ActivityRecipes extends NavigationActivity {

	private TextView textviewDishes;
	private LinearLayout frameDishes;

	private TextView textviewMasalas;
	private LinearLayout frameMasalas;

	private LinearLayout layoutFavShowAll;
	private TextView textviewFavShowAll;
	private ImageView imageviewFavShowAll;
	public static boolean isShowAll = true;

	private LinearLayout layoutMakeRecipe;

	private ListView listviewRecipes;
	public static List<Recipe> recipesList;
	public static List<Recipe> persistantRecipesList;
	private static RecipeListAdapter recipeListAdapter;

	private ListView listvewRecipesMasalas;
	public static List<Recipe> recipesListMasalas;
	public static List<Recipe> persistantRecipesListMasalas;
	private static RecipeListAdapter recipeListAdapterMasalas;

	public static EditText recipesSearchBox;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_recipes);

		// Set up contents
		setContents();
	}

	private void setContents() {
		frameDishes = (LinearLayout) findViewById(R.id.frame_dishes);
		frameMasalas = (LinearLayout) findViewById(R.id.frame_masalas);

		layoutFavShowAll = (LinearLayout) findViewById(R.id.layout_favorites_show_all);
		textviewFavShowAll = (TextView) findViewById(R.id.tv_favorites_show_all);
		imageviewFavShowAll = (ImageView) findViewById(R.id.iv_favorites_show_all);
		layoutFavShowAll.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (isShowAll) {
					isShowAll = false;
					imageviewFavShowAll.setImageResource(R.drawable.ic_search);
					textviewFavShowAll.setText(getResources().getString(R.string.show_all));

					getRecipesFavList();

				} else {
					isShowAll = true;
					imageviewFavShowAll.setImageResource(R.drawable.ic_fav_search);
					textviewFavShowAll.setText(getResources().getString(R.string.favorites));

					getRecipesList();
				}
			}
		});

		layoutMakeRecipe = (LinearLayout) findViewById(R.id.layout_make_recipe);
		layoutMakeRecipe.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(ActivityRecipes.this.getParent(), ActivityCreateNewRecipe.class);
				goNextHistory("Create Recipe", intent);
			}
		});

		textviewDishes = (CustomFontTextView) findViewById(R.id.tv_dishes);
		textviewDishes.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				textviewDishes.setBackgroundColor(getResources().getColor(R.color.bg_tab_selected));
				textviewDishes.setTextColor(getResources().getColor(R.color.white));

				textviewMasalas.setBackgroundColor(getResources().getColor(R.color.bg_tab_normal));
				textviewMasalas.setTextColor(getResources().getColor(R.color.gray));

				frameDishes.setVisibility(View.VISIBLE);
				frameMasalas.setVisibility(View.GONE);

				layoutFavShowAll.setVisibility(View.VISIBLE);
				layoutMakeRecipe.setVisibility(View.VISIBLE);
				
				recipesSearchBox.setText("");
			}
		});

		textviewMasalas = (CustomFontTextView) findViewById(R.id.tv_masalas);
		textviewMasalas.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				textviewMasalas.setBackgroundColor(getResources().getColor(R.color.bg_tab_selected));
				textviewMasalas.setTextColor(getResources().getColor(R.color.white));

				textviewDishes.setBackgroundColor(getResources().getColor(R.color.bg_tab_normal));
				textviewDishes.setTextColor(getResources().getColor(R.color.gray));

				frameDishes.setVisibility(View.GONE);
				frameMasalas.setVisibility(View.VISIBLE);

				layoutFavShowAll.setVisibility(View.GONE);
				layoutMakeRecipe.setVisibility(View.GONE);
				
				recipesSearchBox.setText("");
			}
		});

		listviewRecipes = (ListView) frameDishes.findViewById(R.id.lv_recipes);
		recipesList = new ArrayList<Recipe>();
		recipeListAdapter = new RecipeListAdapter(ActivityRecipes.this, R.layout.list_item_recipe, recipesList);
		listviewRecipes.setAdapter(recipeListAdapter);

		listvewRecipesMasalas = (ListView) frameMasalas.findViewById(R.id.lv_recipes);
		recipesListMasalas = new ArrayList<Recipe>();
		recipeListAdapterMasalas = new RecipeListAdapter(ActivityRecipes.this, R.layout.list_item_recipe, recipesListMasalas);
		recipeListAdapterMasalas.setMasalas(true);
		listvewRecipesMasalas.setAdapter(recipeListAdapterMasalas);

		// Get recipe list
		getRecipesList();

		// Get recipe list masalas
		getRecipesListMasalas();

		recipesSearchBox = (EditText) findViewById(R.id.et_search_box);
		recipesSearchBox.setHintTextColor(Color.WHITE);
		recipesSearchBox.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void afterTextChanged(Editable s) {
				String keyword = s.toString();
				updateRecipesListByKeyword(keyword);
//				if (keyword == null || keyword.length() == 0) {
//					recipesSearchBox.setHi
//				}
			}
		});
	}

	private void updateRecipesListByKeyword(String keyword) {
		if (frameMasalas.getVisibility() != View.VISIBLE) {
			if (recipesList != null) {
				recipesList.clear();
			}

			if (keyword != null && keyword.length() > 0) {
				int length = persistantRecipesList.size();
				for (int i = 0; i < length; i++) {
					if (persistantRecipesList.get(i).getName().toLowerCase().contains(keyword)) {
						recipesList.add(persistantRecipesList.get(i));
					}
				}

				recipeListAdapter.notifyDataSetChanged();
			} else {
				int length = persistantRecipesList.size();
				for (int i = 0; i < length; i++) {

					recipesList.add(persistantRecipesList.get(i));
				}

				recipeListAdapter.notifyDataSetChanged();
			}
		} else {
			if (recipesListMasalas != null) {
				recipesListMasalas.clear();
			}

			if (keyword != null && keyword.length() > 0) {
				int length = persistantRecipesListMasalas.size();
				for (int i = 0; i < length; i++) {
					if (persistantRecipesListMasalas.get(i).getName().toLowerCase().contains(keyword)) {
						recipesListMasalas.add(persistantRecipesListMasalas.get(i));
					}
				}

				recipeListAdapterMasalas.notifyDataSetChanged();
			} else {
				int length = persistantRecipesListMasalas.size();
				for (int i = 0; i < length; i++) {

					recipesListMasalas.add(persistantRecipesListMasalas.get(i));
				}

				recipeListAdapterMasalas.notifyDataSetChanged();
			}
		}
	}
	public void onActivityResult(int requestCode, int resultCode, Intent data) {

	}

	public static void getRecipesListMasalas() {
		Shared.dbAdapter.getRecipesMasalas();

		recipeListAdapterMasalas.notifyDataSetChanged();
	}

	public static void getRecipesList() {
		Shared.dbAdapter.getRecipes(false);
		recipeListAdapter.notifyDataSetChanged();
	}

	public static void getRecipesFavList() {
		Shared.dbAdapter.getRecipes(true);
		recipeListAdapter.notifyDataSetChanged();
	}

}